package com.amita.ez.web;

import com.amita.ez.base.Const;
import com.amita.ez.entity.SysDictDetail;
import com.amita.ez.service.ISysDictDetailService;
import com.amita.ez.util.ExcelUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


/**
 * auto generated
 * Fri Jun  4 17:58:34 2021
 */
@RestController
@RequestMapping("/api/sysdictdetail")
public class SysDictDetailCtrl  extends BaseCtrl {


    @Autowired
    ISysDictDetailService sysDictDetailService;


    /**
     * queryWrapper
     * @param query
     * @return
     */
    private QueryWrapper<SysDictDetail> getQueryWrapper(SysDictDetail query, String sort) {
        QueryWrapper<SysDictDetail> queryWrapper = new QueryWrapper<>();

        Long dictId = query.getDictId();
        if(dictId != null){
            queryWrapper.lambda().eq(SysDictDetail::getDictId, dictId);
        }
//        String deptNo = query.getDeptNo();
//        if(StringUtils.isNotBlank(deptNo)){
//            queryWrapper.eq("dept_no",deptNo);
//        }
//        String deptName = query.getDeptName();
//        if(StringUtils.isNotBlank(deptName)){
//            queryWrapper.like("dept_name", deptName);
//        }

//        queryWrapper.lambda().orderByAsc(SysDictDetail::getRealclass);
        if(StringUtils.isNotBlank(sort)){
            sort = sort.replace(",asc", "");
            if(sort.endsWith(",desc")){
                queryWrapper.orderByDesc(sort.replace(",desc", ""));
            }else {
                queryWrapper.orderByAsc(sort);
            }
        }

        queryWrapper.lambda().orderByAsc(SysDictDetail::getDictSort);
        return queryWrapper;
    }

    /**
     * 查询全部 (最多返回 100条记录)
     * @return
     */
    @GetMapping("/all")
    public Object all(SysDictDetail query, String sort){
        IPage<SysDictDetail> pageCondition = new Page(1, 1000);
        QueryWrapper<SysDictDetail> queryWrapper = getQueryWrapper(query, sort);
        IPage<SysDictDetail> result = sysDictDetailService.page(pageCondition, queryWrapper);
        List<SysDictDetail> records = result.getRecords();

        return records;
    }
    /**
     * 查询 分页
     * @param page
     * @param size
     * @return
     */
    @GetMapping()
    public Object find(@RequestParam(value = "page",defaultValue = "1")int page,
                       @RequestParam(value = "size",defaultValue = "10")int size,

                       SysDictDetail query,String sort){
        IPage<SysDictDetail> pageCondition = new Page(page, size);
        QueryWrapper<SysDictDetail> queryWrapper = getQueryWrapper(query, sort);
        IPage<SysDictDetail> result = sysDictDetailService.page(pageCondition, queryWrapper);
        return result;
    }


    /**
     * 查询 详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Object findOne(@PathVariable("id") String id){
        SysDictDetail item = sysDictDetailService.getById(id);
        return item;
    }

    /**
     * 保存/更新
     * @param sysDictDetail
     * @return
     */
    @PostMapping()
    public Object saveIt (SysDictDetail sysDictDetail){

        boolean flag = sysDictDetailService.saveOrUpdate(sysDictDetail);
        Assert.isTrue(flag, Const.CRUD_FAIL_MSG);
        return R.ok("操作成功");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @PostMapping("/d")
    public void remove(String ids){
        boolean flag =  sysDictDetailService.removeByIds(Arrays.asList(ids.split(",")));
        Assert.isTrue(flag, Const.CRUD_FAIL_MSG);

    }


    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/import")
    public void doImport(MultipartFile file){

//        Assert.notNull(file, "id should not null");
//        if(file.isEmpty()){
//            throw new RuntimeException("file is empty");
//        }

    }

    /**
     * 导出
     * @param query
     */
    @GetMapping("/export")
    public void doExport(SysDictDetail query, String sort, HttpServletResponse response) throws IOException {
        IPage pageCondition = new Page(1, 1000);
        QueryWrapper<SysDictDetail> queryWrapper = getQueryWrapper(query, sort);
        IPage result = sysDictDetailService.page(pageCondition, queryWrapper);
        List records = result.getRecords();

        ExcelUtils.exportExcel(records, "SysDictDetail表", null, SysDictDetail.class, "SysDictDetail", response);
    }


}
